{#
This file is part of EC-CUBE

Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.

http://www.ec-cube.co.jp/

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#}
{% extends 'default_frame.twig' %}

{% set menus = ['product', 'class_name'] %}

{% block title %}商品管理{% endblock %}
{% block sub_title %}規格編集{% endblock %}

{% form_theme form 'Form/bootstrap_3_horizontal_layout.html.twig' %}

{% block javascript %}
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.core.min.js"></script>
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.widget.min.js"></script>
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.mouse.min.js"></script>
    <script src="{{ app.config.admin_urlpath }}/assets/js/vendor/jquery.ui/jquery.ui.sortable.min.js"></script>
    <script>
        $(function() {
            var oldRanks = [];
            // 画面の中のrank一覧を保持
            $("div.tableish > .item_box").each(function() {
                oldRanks.push(this.dataset.rank);
            });
            // rsort
            oldRanks.sort(function(a, b) {
                return a - b;
            }).reverse();

            $("div.tableish").sortable({
                items: '> .item_box',
                cursor: 'move',
                update: function(e, ui) {
                    $("body").append($('<div class="modal-backdrop in"></div>'));
                    updateRank();
                }
            });

            var updateRank = function() {
                // 並び替え後にrankを更新
                var newRanks = {};
                var i = 0;
                $("div.tableish > .item_box").each(function() {
                    newRanks[this.dataset.categoryId] = oldRanks[i];
                    i++;
                });

                $.ajax({
                    url: '{{ url('admin_product_class_category_rank_move') }}',
                    type: 'POST',
                    data: newRanks,
                }).done(function(data) {
                    console.log(data);
                    $(".modal-backdrop").remove();
                }).fail(function() {
                    console.log('fail');
                    $(".modal-backdrop").remove();
                });
            };
        });
    </script>
{% endblock %}

{% block main %}
    <div id="edit_wrap" class="row">
        <div id="edit_list_box" class="col-md-12">
            <div id="edit_list_box__body" class="box">
                <div id="edit_box__header" class="box-header">
                    <h3 class="box-title">規格名： {{ ClassName.name }}</h3>
                </div>
                <div id="edit_box__body" class="box-body">
                    <form role="form" class="form-horizontal" name="form1" id="form1" method="post" action="{% if TargetClassCategory.id %}{{ url('admin_product_class_category_edit', {class_name_id: ClassName.id, id: TargetClassCategory.id}) }}{% else %}{{ url('admin_product_class_category', {'class_name_id': ClassName.id}) }}{% endif %}">
                        <div class="form-group">
                            <div class="col-md-12 form-inline">
                                {{ form_widget(form._token) }}
                                {{ form_widget(form.name, {attr: {placeholder: '分類名を入力'}}) }}
                                <button class="btn btn-default btn-sm" type="submit">分類作成</button>
                            </div>
                        </div>
                        <div class="extra-form">
                            {% for f in form.getIterator %}
                                {% if f.vars.name matches '[^plg*]' %}
                                    {{ form_label(f) }}
                                    {{ form_widget(f) }}
                                    {{ form_errors(f) }}
                                {% endif %}
                            {% endfor %}
                        </div>
                    </form>
                </div><!-- /.box-header -->
                {% if ClassCategories|length > 0 %}
                    <div id="sortable_list_box" class="box-body no-padding no-border">
                        <div id="list_box" class="sortable_list">
                            <div id="sortable_list_box__body" class="tableish">

                                {% for ClassCategory in ClassCategories %}

                                    <div id="sortable_list_box__item--{{ ClassCategory.id }}" class="item_box tr" data-category-id="{{ ClassCategory.id }}" data-rank="{{ ClassCategory.rank }}">
                                        <div class="icon_sortable td">
                                            <svg class="cb cb-ellipsis-v">
                                                <use xlink:href="#cb-ellipsis-v"/>
                                            </svg>
                                        </div>
                                        <div id="sortable_list_box__name--{{ ClassCategory.id }}" class="item_pattern td">
                                            <a href="{{ url('admin_product_class_category_edit', {class_name_id: ClassName.id, id: ClassCategory.id}) }}">
                                                {{ ClassCategory.name }}
                                            </a>
                                        </div>
                                        <div id="sortable_list_box__menu_box--{{ ClassCategory.id }}" class="icon_edit td">
                                            <div id="sortable_list_box__menu_box_toggle--{{ ClassCategory.id }}" class="dropdown">
                                                <a class="dropdown-toggle" data-toggle="dropdown"><svg class="cb cb-ellipsis-h"><use xlink:href="#cb-ellipsis-h"/></svg></a>
                                                <ul id="sortable_list_box__menu--{{ ClassCategory.id }}" class="dropdown-menu dropdown-menu-right">
                                                    {% if ClassCategory.id != TargetClassCategory.id %}
                                                        <li>
                                                            <a href="{{ url('admin_product_class_category_edit', {class_name_id: ClassName.id, id: ClassCategory.id}) }}">編集</a>
                                                        </li>
                                                    {% else %}
                                                        <li><a>編集中</a></li>
                                                    {% endif %}
                                                    <li>
                                                        <a href="{{ url('admin_product_class_category_delete', {class_name_id: ClassName.id, id: ClassCategory.id }) }}" {{ csrf_token_for_anchor() }}
                                                           data-method="delete"
                                                           data-message="分類名を削除すると、その分類を利用している商品規格が無効になります。整合性の問題を把握し、バックアップを行ってから削除することを推奨致します。">削除</a>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                    </div><!-- /.item_box -->
                                {% endfor %}

                            </div>
                        </div>
                    </div><!-- /.box-body -->
                {% else %}
                    <div  id="sortable_list_box" class="box-body no-padding">
                        <div id="sortable_list_box__not_find_message" class="data-empty">
                            <svg class="cb cb-inbox"><use xlink:href="#cb-inbox"/></svg>
                            <p>データはありません</p>
                        </div>
                    </div><!-- /.box-body -->
                {% endif %}
                <!-- ▲ データがある時 ▲ -->
            </div><!-- /.box -->
        </div><!-- /.col -->
    </div>
    <div id="edit_list__footer" class="row">
        <div id="edit_list__back_button" class="col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-3 text-center btn_area">
            <p><a href="{{ url('admin_product_class_name') }}">規格一覧に戻る</a></p>
        </div>
    </div>

{% endblock %}
